package cn.change360.newappchildrenenglish;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.change360.newappchildrenenglish.utils.MathematicsUtil;
import cn.change360.newappchildrenenglish.widget.WidgetLetter;

/*
* 学字母
* */

public class LetterActivity extends AppCompatActivity implements View.OnClickListener {

    ImageButton topbar_back;
    TextView topbar_title;

    RelativeLayout word_box;
    ImageButton btn_refresh;
    List<WidgetLetter> wordList;
    Animation animationx, animationy;

    String[] words = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_letter);

        wordList = new ArrayList<>();
        animationx = AnimationUtils.loadAnimation(this, R.anim.shake_x);
        animationy = AnimationUtils.loadAnimation(this, R.anim.shake_y);

        initView();
    }

    private void initView() {
        topbar_back = (ImageButton) findViewById(R.id.topbar_back);
        topbar_title = (TextView) findViewById(R.id.topbar_title);
        word_box = (RelativeLayout) findViewById(R.id.word_box);

        wordList.add((WidgetLetter) findViewById(R.id.word1));
        wordList.add((WidgetLetter) findViewById(R.id.word2));
        wordList.add((WidgetLetter) findViewById(R.id.word3));
        wordList.add((WidgetLetter) findViewById(R.id.word4));
        wordList.add((WidgetLetter) findViewById(R.id.word5));
        wordList.add((WidgetLetter) findViewById(R.id.word6));
        wordList.add((WidgetLetter) findViewById(R.id.word7));

        btn_refresh = (ImageButton) findViewById(R.id.btn_refresh);

        topbar_title.setText("学字母");
        topbar_back.setOnClickListener(this);
        btn_refresh.setOnClickListener(this);
        for (WidgetLetter view : wordList) {
            view.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.topbar_back:
                finish();
                break;
            case R.id.btn_refresh:
                index++;
                setWord(index);
                break;
        }
        if (view instanceof WidgetLetter) {
            WidgetLetter v = (WidgetLetter) view;
            Intent intent = new Intent(this, LetterShowActivity.class);
            intent.putExtra("word", v.getText());
            startActivity(intent);
        }
    }

    private void setWord(int index) {
        if (index > 3) {
            this.index = 0;
            index = 0;
        }
        for (int i = 0; i < wordList.size(); i++) {
            if ((i + (index * 7)) < 26) {
                wordList.get(i).setVisibility(View.VISIBLE);
                wordList.get(i).setText(words[i + (index * 7)]);
                if (MathematicsUtil.getRandom(0, 3) == 1) {
                    wordList.get(i).startAnimation(animationx);
                } else {
                    wordList.get(i).startAnimation(animationy);
                }
            } else {
                wordList.get(i).setVisibility(View.GONE);
            }
        }
    }
}
